<?php

namespace app\index\controller;
// 该文件的位于application\index\controller文件夹
use app\common\model\Teacher;

// 教师模型
use think\Controller;
use think\Request;

/**
 * 教师管理
 */
class TeacherController extends IndexController
{


    public function index()
    {
        try {

            //获取查询信息
            $name = Request::instance()->get('name');

            // $Teacher 首写字母大写，说明它是一个对象，更确切一些说明这是基于Teacher这个模型被我们手工实例化得到的，如果存在teacher数据表，它将对应teacher数据表。
            $pageSize = 5;
            $Teacher = new Teacher;

            //调用分页函数
            $teachers = $Teacher->paginate($pageSize, false, ['query' => ['name' => $name],]);

            // 向V层传数据
            $this->assign('teachers', $teachers);

            // 取回打包后的数据
            $htmls = $this->fetch();

            // 将数据返回给用户
            return $htmls;
        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;
        } catch (\Exception $e) {
            //throw $e
            return '系统错误' . $e->getMessage();
        }

    }

    public function add()
    {
        try {
            //不做其他操作，只展示form表单页面
            $htmls = $this->fetch();
            return $htmls;
        } catch (\Exception $e) {
            return '系统错误' . $e->getMessage();
        }

    }

    /**
     * 插入数据
     */
    public function insert()
    {
        /*        // 新建测试数据
                $teacher = [];
                $teacher['name'] = '王五';
                $teacher['username'] = 'wangwu';
                $teacher['sex'] = '1';
                $teacher['email'] = 'wangwu@yunzhi.club';

                // 引用teacher数据表对应的模型
                $Teacher = new Teacher();

                // 向teacher表中插入数据并判断是否插入成功
                $Teacher->data($teacher)->save();
                return $teacher['name'] . '成功增加至数据表中';*/
        //接收数据
        //Request::instance()返回了一个对象，调用这个对象的post()方法，得到post数据
        //$postData = Request::instance()->post();

        $message = ''; //提示信息

        try {
            $postData = $this->request->post();
            //halt($postData);

            //实例化Teacher空对象
            $Teacher = new Teacher();

            //为对象赋值
            $Teacher->name = $postData['name'];
            $Teacher->username = $postData['username'];
            $Teacher->sex = $postData['sex'];
            $Teacher->email = $postData['email'];
            //$Teacher->create_time = $postData['create_time'];

            //新增对象至数据表
            $result = $Teacher->validate(true)->save();

            //反馈结果
            if (false === $result) {
                return '新增失败' . $Teacher->getError();
            } else {
                return $this->success('用户：' . $Teacher->name . '新增成功。', url('index'));
            }
        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return $this->error($message);

    }

    public function delete()
    {
        try {
            //获取get数据
            $Request = Request::instance();
            $id = Request::instance()->param('id/d');

            //判断是否成功接收
            if (0 === $id) {
                throw new \Exception("未获取到ID信息", 1);
            }

            //获取要删除的对象
            $Teacher = Teacher::get($id);

            //要删除的对象存在
            if (is_null($Teacher)) {
                throw new \Exception('不存在id为' . $id . '的教师，删除失败', 1);
            }

            //删除对象
            if (!$Teacher->delete()) {
                $message = '删除失败: ' . $Teacher->getError();
            }


        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;
        } catch (\Exception $e) {
            return $e->getMessage();
        }

        //进行跳转
        return $this->success('删除成功', $Request->header('reeferer'));

    }

    public function edit()
    {
        try {
            //获取传入ID
            $id = Request::instance()->param('id/d');

            //在Teacher表模型中获取当前记录
            $Teacher = Teacher::get($id);
            if (is_null($Teacher)) {
                return $this->error('不存在id为' . $id . '的教师，无法修改');
            }

            //将数据传给V层
            $this->assign('Teacher', $Teacher);

            //获取封装好的V层内容
            $htmls = $this->fetch();
            //将封装好的V层内容返回给用户
            return $htmls;
        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;
        } catch (\Exception $e) {
            return $e->getMessage();
        }


    }

    public function update()
    {
        try {
            //接收数据
            $id = Request::instance()->post('id/d'); //获取要更新的数据ID
            $teacher = Request::instance()->post(); //获取要更新的全部数据

            //将数据存入Teacher表
            $Teacher = Teacher::get($id); //获得当前数据库中对应ID的数据
            if (!is_null($Teacher)) {
                //save中必须写入post传过来的数据对象，否则validate不起作用
                if (false === $Teacher->validate(true)->save($teacher)) {
                    return $this->error('更新失败' . $Teacher->getError());
                }
            } else {
                throw new \Exception("所更新的记录不存在", 1);
            }

        } catch (\think\Exception\HttpResponseException $e) {
            throw $e;
        } catch (\Exception $e) {
            return $e->getMessage();
        }
        return $this->success('操作成功', url('index'));

    }


}
